<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>tab栏切换</title>
    <style>
      * {
        margin: 0;
        padding: 0;

      }

      .tab {
        width: 590px;
        height: 340px;
        margin: 20px;
        border: 1px solid #e4e4e4;
        overflow: hidden;
      }

      .tab-nav {
        width: 100%;
        height: 60px;
        line-height: 60px;
        display: flex;
        justify-content: space-between;
        position: relative;
      }

      .tab-nav h3 {
        font-size: 24px;
        font-weight: normal;
        margin-left: 20px;
      }

      .tab-nav ul {
        list-style: none;
        display: flex;
        justify-content: flex-end;
      }

      .tab-nav ul li {
        margin: 0 20px;
        font-size: 14px;
      }

      .tab-nav ul li a {
        text-decoration: none;
        color: #333;
      }

      .tab-nav ul li a.active {
        color: #e1251b;
      }
      .tab-content {
        padding: 0 16px;
      }

      .tab-content .item {
        display: none;
      }

      .tab-content .item.active {
        display: block;
      }
      .tab-nav-line {
        width: 30px;
        height: 2px;
        background-color: #e1251b;
        position: absolute;
        bottom: 15px;
        left: 0;
        transition: all 0.3s;
      }
    </style>
  </head>

  <body>
    <div class="tab">
      <div class="tab-nav">
        <h3>每日特价</h3>
        <ul class="tab-nav-ul">
          <li><a class="active" href="javascript:;" data-id="0">精选</a></li>
          <li><a href="javascript:;" data-id="1">美食</a></li>
          <li><a href="javascript:;" data-id="2">百货</a></li>
          <li><a href="javascript:;" data-id="3">个护</a></li>
          <li><a href="javascript:;" data-id="4">预告</a></li>
        </ul>
        <div class="tab-nav-line"></div>
      </div>
      <div class="tab-content">
        <div class="item active"><img src="./images/tab00.png" alt="" /></div>
        <div class="item"><img src="./images/tab01.png" alt="" /></div>
        <div class="item"><img src="./images/tab02.png" alt="" /></div>
        <div class="item"><img src="./images/tab03.png" alt="" /></div>
        <div class="item"><img src="./images/tab04.png" alt="" /></div>
      </div>
    </div>
    <script>
      // 1. 第一种：遍历所有a标签
      // const as = document.querySelectorAll('.tab-nav a')
      // for (let i = 0; i < as.length; i++) {
      //   as[i].addEventListener('mouseenter', function () {
      //     // 绑定鼠标经过事件
      //     document.querySelector('.tab-nav .active').classList.remove('active')
      //     this.classList.add('active')

      //     document.querySelector('.tab-content .active').classList.remove('active')
      //     document.querySelectorAll('.tab-content .item')[i].classList.add('active')
      //   })
      // }

      // 2. 第二种：事件委托
      const tabNavLine = document.querySelector(".tab-nav-line");
      const firstA = document.querySelector(".tab-nav-ul li:first-child a");
      tabNavLine.style.left = `${firstA.offsetLeft}px`;
      const father = document.querySelector(".tab-nav-ul");
      father.addEventListener("click", function (e) {
        if (e.target.tagName === "A") {
          document
            .querySelector(".tab-nav .active")
            ?.classList.remove("active");
          e.target.classList.add("active");
          console.log("id--------", e.target.dataset.id);
          const id = e.target.dataset.id;
          document
            .querySelector(".tab-content .active")
            ?.classList.remove("active");
          document
            .querySelectorAll(".tab-content .item")
            [id].classList.add("active");
          // 移动tab-nav-line
          tabNavLine.style.left = `${e.target.offsetLeft}px`;
        }
      });
    </script>
  </body>
</html>
